import pymongo
# mongodb操作：https://www.runoob.com/python3/python-mongodb.html
myclient = pymongo.MongoClient('mongodb://test:test@47.99.186.106:27017')
dblist = myclient.list_database_names()
# dblist = myclient.database_names()
print(dblist)
if "test" in dblist:
    print("数据库已存在！")
# mycol = mydb["sites"]

mydb = myclient["test"]
mycollection = mydb["sites"]
mydict = {"name": "RUNOOB", "alexa": "10000", "url": "https://www.runoob.com"}
x = mycollection.insert_one(mydict)

mylist = [
  { "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" },
  { "name": "QQ", "alexa": "101", "url": "https://www.qq.com" },
  { "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" },
  { "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" },
  { "name": "Github", "alexa": "109", "url": "https://www.github.com" }
]
x = mycollection.insert_many(mylist)
# 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)

mylist = [
    {"_id": 1, "name": "RUNOOB", "cn_name": "菜鸟教程"},
    {"_id": 2, "name": "Google", "address": "Google 搜索"},
    {"_id": 3, "name": "Facebook", "address": "脸书"},
    {"_id": 4, "name": "Taobao", "address": "淘宝"},
    {"_id": 5, "name": "Zhihu", "address": "知乎"}
]

x = mycollection.insert_many(mylist)

# 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)
